import pygame.font
from pygame.sprite import Group
from ship import Ship


class Scoreboard:
    """显示评分信息的类"""

    def __init__(self, ai_game):
        """初始化分数涉及属性"""
        self.ai_game = ai_game
        self.screen = ai_game.screen
        self.screen_rect = self.screen.get_rect()
        self.settings = ai_game.settings
        self.stats = ai_game.stats

        # 评分信息的字体设置
        # 设置文本颜色
        self.text_color = (250, 250, 250)
        # 实例化一个字体对象
        self.font = pygame.font.SysFont(None, 48)

        # 准备初始分数图像
        # prep_score 文本转化为图像
        self.prep_score()
        # 将最高分转换为图像
        self.prep_high_score()
        self.prep_level()
        self.prep_ships()

    def prep_score(self):
        """将分数转换为渲染图像"""
        # 将数值转换为字符串
        # score_str = str(self.stats.score)
        # 将分数舍入到最近的10的整数倍
        rounded_score = round(self.stats.score, -1)
        # 将数值转换为字符串，并在其中插入逗号
        score_str = "score:" + "{:,}".format(rounded_score)
        # 将字符串传递给创建图像的render
        self.score_image = self.font.render(score_str, True, self.text_color, self.settings.bg_color)

        # 在屏幕的右上角显示得分
        self.score_rect = self.score_image.get_rect()
        self.score_rect.right = self.screen_rect.right - 20
        self.score_rect.top = 20

    def prep_high_score(self):
        """将最高分转换为渲染图像。"""

        # round将得分舍入到最近的10的整数倍，并用逗号表示千分位分隔符
        high_score = round(self.stats.high_score, -1)
        high_score_str = "highestScore:" + "{:,}".format(high_score)
        # 生成图像
        self.high_score_image = self.font.render(high_score_str, True, self.text_color, self.settings.bg_color)

        # 将最高分放在屏幕顶部中央
        self.high_score_rect = self.high_score_image.get_rect()
        # 居中
        self.high_score_rect.centerx = self.screen_rect.centerx
        self.high_score_rect.top = self.score_rect.top

    def prep_level(self):
        """将等级渲染为图片"""
        # 将数值转换为字符串
        level_str = "level:" + str(self.stats.level)
        # 生成图像
        self.level_image = self.font.render(level_str, True, self.text_color, self.settings.bg_color)

        # 将等级放在得分下面
        self.level_rect = self.level_image.get_rect()
        self.level_rect.right = self.score_rect.left - 20
        self.level_rect.top = self.score_rect.top

    def prep_ships(self):
        """显示余下飞船"""
        # 创建编组，用于存储飞船实例
        self.ships = Group()
        for ship_number in range(self.stats.ships_left):
            ship = Ship(self.ai_game)
            # 设置飞船的坐标边距
            ship.rect.x = 10 + ship_number * ship.rect.width
            ship.rect.y = 10
            # 将飞船添加到编组中
            self.ships.add(ship)

    def check_high_score(self):
        """检查是否有新的最高分出现"""
        if self.stats.score > self.stats.high_score:
            self.stats.high_score = self.stats.score
            self.prep_high_score()

    def show_score(self):
        """显示得分"""
        self.screen.blit(self.score_image, self.score_rect)
        self.screen.blit(self.high_score_image, self.high_score_rect)
        self.screen.blit(self.level_image, self.level_rect)
        self.ships.draw(self.screen)
